package JaggSoft;

import JaggSoft.Utilidades.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Proveedores extends javax.swing.JInternalFrame {
    private int codigo_proveedor;
    private boolean nuevo;
    private String id_bitacora;
    private JFileChooser selector;
    public Proveedores() {
        initComponents();
        this.setLocation(new Point(70, 0));
        selector=new JFileChooser();
        selector.setFileFilter(new FileNameExtensionFilter("Libro de Excel",new String[]{"xls","xlsx"}));
        selector.setMultiSelectionEnabled(false);
        selector.setSelectedFile(new File("proveedores.xls"));
        Bloquear(true);
        BloquearTexto(true);
        CargarProveedores();
        codigo_proveedor = -1;
        if(Conexion.bitacora) id_bitacora=Util.IniciarBitacora(this.getTitle());
    }
    private void Bloquear(boolean estado) {
        jbtnNuevo.setEnabled(estado);
        jbtnGuardar.setEnabled(!estado);
        jbtnModificar.setEnabled(estado);
        jbtnEliminar.setEnabled(estado);
        jbtnCancelar.setEnabled(!estado);
        jtblProveedores.setEnabled(estado);
    }
    private void BloquearTexto(boolean estado) {
        jtfNombre.setEditable(!estado);
        jtfTelefono.setEditable(!estado);
        jtfCorreoElectronico.setEditable(!estado);
    }
    private void Limpiar() {
        jtfNombre.setText("");
        jtfTelefono.setText("");
        jtfCorreoElectronico.setText("");
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpProveedores = new javax.swing.JPanel();
        jtfTelefono = new javax.swing.JTextField();
        jtfNombre = new javax.swing.JTextField();
        jtfCorreoElectronico = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jpAcciones = new javax.swing.JPanel();
        jbtnCancelar = new javax.swing.JButton();
        jbtnEliminar = new javax.swing.JButton();
        jbtnModificar = new javax.swing.JButton();
        jbtnGuardar = new javax.swing.JButton();
        jbtnNuevo = new javax.swing.JButton();
        jbtnExportar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtblProveedores = new javax.swing.JTable();
        jchFormatearTelefono = new javax.swing.JCheckBox();

        setClosable(true);
        setMaximizable(true);
        setTitle("Administración de Proveedores");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/proveedores.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jpProveedores.setBorder(javax.swing.BorderFactory.createTitledBorder("Proveedor"));
        jpProveedores.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                jpProveedoresComponentResized(evt);
            }
        });

        jtfTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtfTelefonoKeyTyped(evt);
            }
        });

        jLabel10.setText("Nombre");

        jLabel9.setText("Correo Electronico");

        jLabel11.setText("Telefono");

        jpAcciones.setBorder(javax.swing.BorderFactory.createTitledBorder("Acciones"));

        jbtnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_cancelar.png"))); // NOI18N
        jbtnCancelar.setToolTipText("Cancelar");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_eliminar.png"))); // NOI18N
        jbtnEliminar.setToolTipText("Eliminar");
        jbtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEliminarActionPerformed(evt);
            }
        });

        jbtnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_modificar.png"))); // NOI18N
        jbtnModificar.setToolTipText("Modificar");
        jbtnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnModificarActionPerformed(evt);
            }
        });

        jbtnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_guardar.png"))); // NOI18N
        jbtnGuardar.setToolTipText("Guardar");
        jbtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGuardarActionPerformed(evt);
            }
        });

        jbtnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_nuevo.png"))); // NOI18N
        jbtnNuevo.setToolTipText("Nuevo");
        jbtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNuevoActionPerformed(evt);
            }
        });

        jbtnExportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/excel.png"))); // NOI18N
        jbtnExportar.setToolTipText("Exportar a Excel");
        jbtnExportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnExportarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpAccionesLayout = new javax.swing.GroupLayout(jpAcciones);
        jpAcciones.setLayout(jpAccionesLayout);
        jpAccionesLayout.setHorizontalGroup(
            jpAccionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpAccionesLayout.createSequentialGroup()
                .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnExportar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpAccionesLayout.setVerticalGroup(
            jpAccionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnExportar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jScrollPane2.setViewportView(jtblProveedores);

        jchFormatearTelefono.setSelected(true);
        jchFormatearTelefono.setText("Auto formatear");

        javax.swing.GroupLayout jpProveedoresLayout = new javax.swing.GroupLayout(jpProveedores);
        jpProveedores.setLayout(jpProveedoresLayout);
        jpProveedoresLayout.setHorizontalGroup(
            jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpProveedoresLayout.createSequentialGroup()
                .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpProveedoresLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(jpProveedoresLayout.createSequentialGroup()
                        .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jpProveedoresLayout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jpProveedoresLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jpProveedoresLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel9)))
                        .addGap(18, 18, 18)
                        .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfNombre)
                            .addComponent(jtfCorreoElectronico)
                            .addGroup(jpProveedoresLayout.createSequentialGroup()
                                .addComponent(jtfTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jchFormatearTelefono)))))
                .addContainerGap())
            .addGroup(jpProveedoresLayout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addComponent(jpAcciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpProveedoresLayout.setVerticalGroup(
            jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpProveedoresLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCorreoElectronico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(jchFormatearTelefono))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpAcciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpProveedores, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpProveedores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void CargarProveedores() {
        Util.CargarTabla(jtblProveedores,"SELECT id_proveedor,nombre,numero_contacto, email FROM proveedor",new String[]{"Id","Nombre","Telefono","E-mail"});
        Util.OcultarColumna(jtblProveedores,0);
        Util.AlinearColumnas(jtblProveedores,SwingConstants.CENTER);
    }
    private void SeleccionarDatos() {
        int fila = jtblProveedores.getSelectedRow();
        if (fila != -1) {
            codigo_proveedor = (int) jtblProveedores.getValueAt(fila, 0);
            jtfNombre.setText((String) jtblProveedores.getValueAt(fila, 1));
            jtfTelefono.setText((String) jtblProveedores.getValueAt(fila, 2));
            jtfCorreoElectronico.setText((String) jtblProveedores.getValueAt(fila, 3));
        }
    }
    private int ContarNumeros(String texto){
        int n=0;
        for(char c:texto.toCharArray())
            if("0123456789".contains(""+c))
                n++;
        return n;
    }
    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        Limpiar();
        Bloquear(true);
        BloquearTexto(true);
        SeleccionarDatos();
    }//GEN-LAST:event_jbtnCancelarActionPerformed
    private void jbtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarActionPerformed
        if (jtblProveedores.getSelectedRow() != -1) {
            if (JOptionPane.showConfirmDialog(null, "¿Esta seguro de eliminar el proveedor?", "Administración de Proveedores", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                if (Conexion.EjecutarConsulta("DELETE FROM proveedor Where id_proveedor=" + jtblProveedores.getValueAt(jtblProveedores.getSelectedRow(), 0).toString())) {
                    if(Conexion.bitacora) Util.AccionBitacora(id_bitacora,"Eliminar Proveedor: "+Util.ComponerTexto(jtblProveedores.getValueAt(jtblProveedores.getSelectedRow(), 1).toString()));
                    CargarProveedores();
                    JOptionPane.showMessageDialog(null, "¡Proveedor Eliminado!", "Administración de Proveedores", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un proveedor.", "Administración de Proveedores", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jbtnEliminarActionPerformed
    private void jbtnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnModificarActionPerformed
        jtfNombre.setText((String) jtblProveedores.getValueAt(jtblProveedores.getSelectedRow(), 1));
        jtfTelefono.setText((String) jtblProveedores.getValueAt(jtblProveedores.getSelectedRow(), 2));
        jtfCorreoElectronico.setText((String) jtblProveedores.getValueAt(jtblProveedores.getSelectedRow(), 3));
        Bloquear(false);
        BloquearTexto(false);
        nuevo = false;
    }//GEN-LAST:event_jbtnModificarActionPerformed
    private boolean ValidarCampos(){
        String msj="";
        msj=(jtfNombre.getText().equals("")?"Falta ingresar el nombre.":"");
        msj+=(jtfCorreoElectronico.getText().equals("")?(!msj.equals("")?", el correo electrónico":"Falta ingresar el correo electrónico"):"");
        msj+=(jtfTelefono.getText().equals("")?(!msj.equals("")?", el teléfono":"Falta ingresar el teléfono"):"");
        if(!msj.equals("")) JOptionPane.showMessageDialog(null,msj+".","Falta ingresar algunos campos",JOptionPane.WARNING_MESSAGE);
        return msj.equals("");
    }
    private void jbtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGuardarActionPerformed
        if(ValidarCampos()){
            if(nuevo){
                if (Conexion.EjecutarConsulta("INSERT INTO proveedor(nombre,numero_contacto,email) VALUES('" + Util.ComponerTexto(jtfNombre.getText())+"','" + Util.ComponerTexto(jtfTelefono.getText())+ "','" + Util.ComponerTexto(jtfCorreoElectronico.getText()) + "');")) {
                    if(Conexion.bitacora) Util.AccionBitacora(id_bitacora,"Guardar Proveedor: "+Util.ComponerTexto(jtfNombre.getText()));
                    CargarProveedores();
                    Bloquear(true);
                    BloquearTexto(true);
                    Limpiar();
                    JOptionPane.showMessageDialog(null, "¡Proveedor Agregado!", "Administración de Proveedores", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "No se pudo agregar el proveedor.", "Administración de Proveedor", JOptionPane.ERROR_MESSAGE);
                }
            }
            else{
                int id = (int) jtblProveedores.getValueAt(jtblProveedores.getSelectedRow(), 0);
                if(jtblProveedores.getSelectedRow()>-1){
                    if (Conexion.EjecutarConsulta("UPDATE proveedor SET nombre='" + Util.ComponerTexto(jtfNombre.getText()) + "',numero_contacto='" + Util.ComponerTexto(jtfTelefono.getText()) + "',email='" + Util.ComponerTexto(jtfCorreoElectronico.getText()) + "' WHERE id_proveedor=" + id)) {
                        if(Conexion.bitacora) Util.AccionBitacora(id_bitacora,"Modificar Proveedor: "+Util.ComponerTexto(jtfNombre.getText()));
                        CargarProveedores();
                        Bloquear(true);
                        BloquearTexto(true);
                        Limpiar();
                        JOptionPane.showMessageDialog(null, "¡Proveedor Modificado!", "Administración de Proveedores", JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "No se pudo modificar el proveedor.", "Administración de Proveedores", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
    }//GEN-LAST:event_jbtnGuardarActionPerformed
    private void jbtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNuevoActionPerformed
        Bloquear(false);
        BloquearTexto(false);
        Limpiar();
        nuevo = true;
    }//GEN-LAST:event_jbtnNuevoActionPerformed
    private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
        if(evt.getComponent().getLocation().x<70)
            evt.getComponent().setLocation(70,evt.getComponent().getLocation().y);
        if(evt.getComponent().getLocation().y<0)
            evt.getComponent().setLocation(evt.getComponent().getLocation().x,0);
    }//GEN-LAST:event_formComponentMoved
    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        if(this.getParent().getWidth()-70<this.getWidth()  && this.isMaximum)
            this.setSize(this.getWidth()-70,this.getHeight());
    }//GEN-LAST:event_formComponentResized
    private void jpProveedoresComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpProveedoresComponentResized
        jpAcciones.setLocation((jpProveedores.getWidth()-jpAcciones.getWidth())/2,jpAcciones.getY());
    }//GEN-LAST:event_jpProveedoresComponentResized
    private void jtfTelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfTelefonoKeyTyped
        if((int)evt.getKeyChar()!=KeyEvent.VK_BACK_SPACE){
            String dato=(jtfTelefono.getText()+evt.getKeyChar()).replaceAll("-","");
            if (!Util.esNumero(dato, false))
                evt.consume();
            if(jchFormatearTelefono.isSelected()){
                if(ContarNumeros(jtfTelefono.getText())==4 && jtfTelefono.getText().length()==4)
                    jtfTelefono.setText(jtfTelefono.getText()+"-");
                if(ContarNumeros(jtfTelefono.getText())>=8)
                    evt.consume();
            }
        }
    }//GEN-LAST:event_jtfTelefonoKeyTyped
    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        if(Conexion.bitacora) Util.ClausurarBitacora(id_bitacora);
    }//GEN-LAST:event_formInternalFrameClosing

    private void jbtnExportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnExportarActionPerformed
        if(jtblProveedores.getRowCount()>0){
            if(selector.showSaveDialog(null)==JOptionPane.OK_OPTION){
                try {
                    String ruta=selector.getSelectedFile().getAbsolutePath();
                    if(!ruta.endsWith(".xls"))
                        ruta+=".xls";
                    File archivo_excel=new File(ruta);
                    switch(Util.ExportarExcel(jtblProveedores,archivo_excel,"Proveedores")){
                        case OK:
                        JOptionPane.showMessageDialog(null,"Archivo Exportado",this.getTitle(),JOptionPane.INFORMATION_MESSAGE);
                        Desktop.getDesktop().open(archivo_excel);
                        break;
                        case CANCELADO:
                        break;
                        case ERROR:
                        JOptionPane.showMessageDialog(null,"No se pudo exportar el archivo","Error",JOptionPane.ERROR_MESSAGE);
                        break;
                    }
                } catch (Exception ex) {System.err.println(ex.getMessage());}
            }
        }
        else
            JOptionPane.showMessageDialog(null,"No hay datos para exportar","Sin Datos",JOptionPane.WARNING_MESSAGE);
    }//GEN-LAST:event_jbtnExportarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JButton jbtnExportar;
    private javax.swing.JButton jbtnGuardar;
    private javax.swing.JButton jbtnModificar;
    private javax.swing.JButton jbtnNuevo;
    private javax.swing.JCheckBox jchFormatearTelefono;
    private javax.swing.JPanel jpAcciones;
    private javax.swing.JPanel jpProveedores;
    private javax.swing.JTable jtblProveedores;
    private javax.swing.JTextField jtfCorreoElectronico;
    private javax.swing.JTextField jtfNombre;
    private javax.swing.JTextField jtfTelefono;
    // End of variables declaration//GEN-END:variables
}
